#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "StdFile.h"
#define true 1
#define false 0
int IsFileExist(const char *FilePath)
{
    FILE * file = fopen(FilePath,"r");
    if(file == NULL)
        return false;
    else
    {
        fclose(file);
        return true;
    }
}

char *LoadFromFile(const char *FilePath)
{
    FILE * file = fopen(FilePath,"r");
    if(file == NULL)
    {
        printf("file %s open fail!\n",FilePath);
        return NULL;
    }
    fseek(file,0,SEEK_END);
    size_t size = ftell(file); //long unsigned int

    char * content = (char *)malloc(size + 1);
    fseek(file,0,SEEK_SET);
    fread(content,size,1,file);
    fclose(file);
    return content;
}
void RemoveCharIndex(char *a,int index)
{
    if(index < 0 || index >= strlen(a))
    {
        printf("invalid placed!\n");
        return;
    }

    for(int i = index;i < strlen(a);i++)
    {
        a[i] = a[i + 1];
    }
}


void RemoveChar(char *a,char element)
{
    int len = strlen(a);
    for(int i= 0;i<len;i++)
    {
        if(a[i] == element)
        {
            RemoveCharIndex(a,i);
            i--;
        }
    }
}



DBL *GetLineFromFile(const char *FilePath)
{
    FILE * file = fopen(FilePath,"r");
    if(file == NULL)
    {
        printf("file %s open fail!\n",FilePath);
        return NULL;
    }

    DBL *list = (DBL*)malloc(sizeof(DBL));
    InitDBLinkList(list);

    char ContentTemp[100] = {0};
    while(fgets(ContentTemp,100,file) != NULL)
    {
        char *Line = (char *)malloc(strlen(ContentTemp) + 1);
        
        strcpy(Line,ContentTemp);
        RemoveChar(Line,'\n');
        DBInsertTail(list,Line);
    }
    fclose(file);
    return list;
}

void WriteToFile(const char *FilePath, void* ptr,size_t size)
{
    FILE * file = fopen(FilePath,"w");
    if(file == NULL)
    {
        printf("WriteToFile open file fail!\n");
        return;
    }
    if(fwrite(ptr,size,1,file) <= 0)
    {
        printf("WriteToFile error!\n");
    }
    fclose(file);
}

void WriteLineToFile(const char *FilePath, DBL *list)
{
    FILE * file = fopen(FilePath,"w");
    if(file == NULL)
    {
        printf("WriteLineToFile open file fail!\n");
        return;
    }

    struct Node * TravelPoint = list->head;
    while (TravelPoint != NULL)
    {
        fputs(TravelPoint->data,file);
        TravelPoint = TravelPoint->next;
    }
    fclose(file);
}

void CopyFile(const char *SourcePath, const char *TargetPath)
{
    if(IsFileExist(SourcePath) == false)
    {
        printf("the sourcefile is not exist or has no read permission!\n");
        return;
    }
    char *fileContent = LoadFromFile(SourcePath);

    if(IsFileExist(TargetPath) == true)
    {
        char Target[100] = {0};
        strcpy(Target,TargetPath);
        char *fileName = strtok(Target,".");
        char *backName = strtok(NULL,".");

        char newPath[100] = {0};
        strcpy(newPath,fileName);
        strcat(newPath,"_new.");
        strcat(newPath,backName);

        if(IsFileExist(newPath) == true)
        {
            CopyFile(SourcePath,newPath);
            return;
        }
            
        WriteToFile(newPath,fileContent,strlen(fileContent));
        free(fileContent);
        return;
    }
    WriteToFile(TargetPath,fileContent,strlen(fileContent));
    free(fileContent);
}

void AppenedToFile(const char * FilePath,void *ptr,size_t size)
{
    FILE *file = fopen(FilePath,"a");
    if(file == NULL)
    {
        printf("AppenedToFile open file error!\n");
        return;
    }
    fwrite(ptr,size,1,file);
    fputc('\n', file);
    fclose(file);
}